package com.biz.primus.management.promotion.controller;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.base.vo.promotion.PromotionProductReqVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.promotion.feign.PromotionFeignClient;
import com.biz.primus.management.promotion.service.PromotionSupportService;
import com.biz.primus.model.promotion.vo.DiscountPromotionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 折扣促销后台Controller
 *
 * @author david-liu
 */
@Controller
@RequestMapping(value = "/management/discountPromotion")
public class DiscountPromotionController extends AbstractBaseController {

    @Autowired
    private PromotionSupportService promotionSupportService;

    @Autowired
    private PromotionFeignClient promotionFeignClient;

    @RequestMapping(value = "list")
    public ModelAndView list(PageVo pageVo) {
        SpringPageVO<DiscountPromotionVo> pageVO = promotionFeignClient.getDiscountPromotions(pageVo);
        return new ModelAndView("discount/list").addObject("page", pageVO);
    }

    @RequestMapping(value = "new")
    public ModelAndView create() {
        return new ModelAndView("discount/add");
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit(Long id) {
        DiscountPromotionVo discountPromotion = promotionFeignClient.getDiscountPromotion(id);
        return new ModelAndView("discount/add").addObject("discountPromotion", discountPromotion);
    }

    @RequestMapping(value = "statusToggle")
    public ModelAndView statusToggle(Long id) {
        if (!promotionFeignClient.toggleDiscountPromotionStatus(id)) {
            throw new BizBusinessException("切换活动状态出错");
        }
        return new ModelAndView("redirect:list.do");
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Boolean save(String data) {
        return promotionFeignClient.updateDiscountPromotion(JsonUtils.json2Obj(data, DiscountPromotionVo.class));
    }

    @RequestMapping(value = "/products")
    @ResponseBody
    public String promotionProducts(Long promotionId) {
        return "";
    }

    @RequestMapping(value = "/products/search")
    @ResponseBody
    public String searchPromotionProducts(PromotionProductReqVo reqVo) {
        return "";
    }

    @RequestMapping(value = "/rates")
    @ResponseBody
    public String rates(Long promotionId) {
        return JsonUtils.obj2Json(promotionSupportService.listDiscountRates(promotionId));
    }
}
